from rest_framework.filters import BaseFilterBackend


class GoodPermissionFilterBackend(BaseFilterBackend):

    def get_perms(self, user):
        ALL_SCHOOL_PERMISSION = (True, [])
        if user.is_superuser:
            print('超级管理员')
            has_all_school_permission = True
            return ALL_SCHOOL_PERMISSION
        print('超级管理员', user.is_superuser)
        permissions = user.get_all_permissions()
        perms = []
        for permission in permissions:
            print(permission)
            # if permission.startswith('school.schoolpermission__'):
            #     perm = permission.split('school.schoolpermission__')[-1]
            #     if perm == 'all':
            #         return ALL_SCHOOL_PERMISSION
            perms.append(permission)
        return (False, perms)

    def filter_queryset(self, request, queryset, view):
        print('超级管理员123')
        user = request.user
        model_cls = queryset.model
        has_all_school_permission, perms = self.get_perms(user)

        if has_all_school_permission:
            return queryset

        queryset = queryset
        return queryset